package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.PingYinUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhPlatformPartsTypeDetail;
import com.ruoyi.zsyh.service.IZsyhPlatformPartsTypeDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品分类Controller
 * 
 * @author yuexl
 * @date 2022-03-17
 */
@Controller
@RequestMapping("/zsyh/partsTypeDetail")
public class ZsyhPlatformPartsTypeDetailController extends BaseController
{
    private String prefix = "zsyh/partsTypeDetail";

    @Autowired
    private IZsyhPlatformPartsTypeDetailService zsyhPlatformPartsTypeDetailService;

    @RequiresPermissions("zsyh:partsTypeDetail:view")
    @GetMapping()
    public String partsTypeDetail()
    {
        return prefix + "/partsTypeDetail";
    }

    /**
     * 查询产品分类列表
     */
    @RequiresPermissions("zsyh:partsTypeDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhPlatformPartsTypeDetail zsyhPlatformPartsTypeDetail)
    {
        startPage();
        List<ZsyhPlatformPartsTypeDetail> list = zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailList(zsyhPlatformPartsTypeDetail);
        return getDataTable(list);
    }

    /**
     * 查询产品分类列表
     */
    @RequiresPermissions("zsyh:partsTypeDetail:list")
    @PostMapping("/listSecond")
    @ResponseBody
    public TableDataInfo listSecond(ZsyhPlatformPartsTypeDetail zsyhPlatformPartsTypeDetail)
    {
        startPage();
        List<ZsyhPlatformPartsTypeDetail> list = zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailSecondList(zsyhPlatformPartsTypeDetail);
        return getDataTable(list);
    }

    /**
     * 导出产品分类列表
     */
    @RequiresPermissions("zsyh:partsTypeDetail:export")
    @Log(title = "产品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhPlatformPartsTypeDetail zsyhPlatformPartsTypeDetail)
    {
        List<ZsyhPlatformPartsTypeDetail> list = zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailList(zsyhPlatformPartsTypeDetail);
        ExcelUtil<ZsyhPlatformPartsTypeDetail> util = new ExcelUtil<ZsyhPlatformPartsTypeDetail>(ZsyhPlatformPartsTypeDetail.class);
        return util.exportExcel(list, "产品分类数据");
    }

    /**
     * 新增产品分类
     */
    @GetMapping(value = { "/add/{typeLevel}", "/add/" })
    public String add(@PathVariable(value = "typeLevel", required = false) Long typeLevel, ModelMap mmap)
    {
        mmap.put("typeLevel",typeLevel);
        if(EmptyUtils.isNotEmpty(typeLevel)){
            if("2".equals(typeLevel.toString())){
                ZsyhPlatformPartsTypeDetail zsyhPartsTypeDetail = new ZsyhPlatformPartsTypeDetail();
                zsyhPartsTypeDetail.setParentId("0");
                List<ZsyhPlatformPartsTypeDetail> zsyhPartsTypeDetailList = zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailList(zsyhPartsTypeDetail);
                mmap.put("types",zsyhPartsTypeDetailList);
                return prefix + "/addSecond";
            }else{
                return prefix + "/add";
            }
        }else{
            return prefix + "/add";
        }
    }

    /**
     * 新增保存产品分类
     */
    @RequiresPermissions("zsyh:partsTypeDetail:add")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhPlatformPartsTypeDetail zsyhPlatformPartsTypeDetail)
    {
        //新增的时候判断是否有重复的名字，如果有重复则不能添加
        ZsyhPlatformPartsTypeDetail zsyhPlatformPartsTypeDetail1 = new ZsyhPlatformPartsTypeDetail();
        zsyhPlatformPartsTypeDetail1.setTypeName(zsyhPlatformPartsTypeDetail.getTypeName());
        if(zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailList(zsyhPlatformPartsTypeDetail1).size()>0){
            return error("已经存在该分类，不能重复添加!");
        }
        //设置类型编码
        zsyhPlatformPartsTypeDetail.setTypeCode("type_"+ PingYinUtil.getFirstSpell(zsyhPlatformPartsTypeDetail.getTypeName()));
        if("0".equals(zsyhPlatformPartsTypeDetail.getParentId()) || EmptyUtils.isEmpty(zsyhPlatformPartsTypeDetail.getParentId())){
            //一级分类，用两位表示，二级分类用三位数表示
            zsyhPlatformPartsTypeDetail.setTypeLevel("1");
            //设置排序
            List<ZsyhPlatformPartsTypeDetail> zsyhPartsTypeDetailList = zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailList(new ZsyhPlatformPartsTypeDetail());
            if(zsyhPartsTypeDetailList.size()>0){
                zsyhPlatformPartsTypeDetail.setTypeSort(zsyhPartsTypeDetailList.get(zsyhPartsTypeDetailList.size()-1).getTypeSort()+1);
            }else{
                zsyhPlatformPartsTypeDetail.setTypeSort(1);
            }
        }else{
            //二级分类
            ZsyhPlatformPartsTypeDetail zsyhPartsType3 = new ZsyhPlatformPartsTypeDetail();
            zsyhPartsType3.setParentId(zsyhPlatformPartsTypeDetail.getParentId());
            List<ZsyhPlatformPartsTypeDetail> zsyhPartsTypeList = zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailList(zsyhPartsType3);
            zsyhPlatformPartsTypeDetail.setTypeLevel("2");
            //设置排序
            if(zsyhPartsTypeList.size()>0){
                zsyhPlatformPartsTypeDetail.setTypeSort(zsyhPartsTypeList.get(zsyhPartsTypeList.size()-1).getTypeSort()+1);
            }else{
                zsyhPlatformPartsTypeDetail.setTypeSort(1);
            }
        }
        return toAjax(zsyhPlatformPartsTypeDetailService.insertZsyhPlatformPartsTypeDetail(zsyhPlatformPartsTypeDetail));
    }

    /**
     * 修改产品分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPlatformPartsTypeDetail zsyhPlatformPartsTypeDetail = zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailById(id);
        mmap.put("zsyhPlatformPartsTypeDetail", zsyhPlatformPartsTypeDetail);
        if("0".equals(zsyhPlatformPartsTypeDetail.getParentId())){
            //一级分类
            return prefix + "/edit";
        }else{
            //二级分类
            ZsyhPlatformPartsTypeDetail zsyhPartsType1 = new ZsyhPlatformPartsTypeDetail();
            zsyhPartsType1.setParentId("0");
            List<ZsyhPlatformPartsTypeDetail> zsyhPartsTypeList = zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailList(zsyhPartsType1);
            for (ZsyhPlatformPartsTypeDetail temp : zsyhPartsTypeList)
            {
                if (zsyhPlatformPartsTypeDetail.getParentId().equals(temp.getId()))
                {
                    temp.setFlag(true);
                    break;
                }
            }
            mmap.put("types",zsyhPartsTypeList);
            return prefix + "/editSecond";
        }
    }

    /**
     * 修改保存产品分类
     */
    @RequiresPermissions("zsyh:partsTypeDetail:edit")
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhPlatformPartsTypeDetail zsyhPlatformPartsTypeDetail)
    {
        ZsyhPlatformPartsTypeDetail zsyhPlatformPartsTypeDetail1 = new ZsyhPlatformPartsTypeDetail();
        zsyhPlatformPartsTypeDetail1.setTypeName(zsyhPlatformPartsTypeDetail.getTypeName());
        List<ZsyhPlatformPartsTypeDetail> zsyhPlatformPartsTypeDetailList = zsyhPlatformPartsTypeDetailService.selectZsyhPlatformPartsTypeDetailList(zsyhPlatformPartsTypeDetail1);
        if(zsyhPlatformPartsTypeDetailList.size()>0){
            //如果是同一个分类，可以修改
            if(!zsyhPlatformPartsTypeDetail.getId().equals(zsyhPlatformPartsTypeDetailList.get(0).getId())){
                return error("已经存在该分类，不能重复添加");
            }
        }
        return toAjax(zsyhPlatformPartsTypeDetailService.updateZsyhPlatformPartsTypeDetail(zsyhPlatformPartsTypeDetail));
    }

    /**
     * 删除产品分类
     */
    @RequiresPermissions("zsyh:partsTypeDetail:remove")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zsyhPlatformPartsTypeDetailService.deleteZsyhPlatformPartsTypeDetailByIds(ids));
    }
}
